<?php

namespace app\index\controller;

use classes\RedisClass;
use classes\TokenBucket;
use QCloud\COSSTS\Sts;
use think\Controller;
use think\Db;
use think\facade\Env;
use think\facade\Cache;
use think\Queue;
use service\WechatService;

class ote extends Controller
{


    public function __construct()
    {

        parent::__construct();

//        if (Env::get('app_debug')) {
//            session('wxd_openid', 'o6rBgxER1najTK-KGVmQSa9iPZig');
//        }

        session('wxd_openid', 'o6rBgxER1najTK-KGVmQSa9iPZig');

//        if (!Env::get('app_debug') && !isMobile()) {
//            tip_alert('请在微信手机端访问！');
//        }

        /*else {
            if (!session('wxd_openid')) {
                $wechat = WechatService::webOauth($this->request->url(true), 0);
                if (!empty($wechat)) {
                    $openid = $wechat['openid'];
                    session('wxd_openid', $openid);

                }

            }
        }*/
    }

    /**
     * 控制器基础方法
     */
    public function initialize()
    {

        //30s数据回传
        $config = RedisClass::get_vote_config_info();

        if (!Cache::store('redis')->handler()->get('config_update')) {

            $view_count = Cache::store('redis')->get('view_count'); //总浏览数
            $total_vcount = Cache::store('redis')->get('total_vcount'); //总投票量

            Db::name('tp_vote')
                ->where(array('id' => 1))
                ->update([
//                    'view_count' => $config['view_count'],
//                    'total_vcount' => $config['total_vcount'],
                    'view_count' => $view_count,
                    'total_vcount' => $total_vcount,
                ]);
            Cache::store('redis')->handler()->set('config_update', 1);
            Cache::store('redis')->handler()->expire('config_update', 60);//设置过期时间
        }



    }

    function auth($url = '')
    {

        if (!empty($url)) {
            session('callback_url', $url);
        }
        if (isset($_GET['code']) && !empty($_GET['code'])) {
            $appid = sysconf('wechat_appid');
            $appsecret = sysconf('wechat_appsecret');
            $code = $_GET['code'];
            $weixinurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code ";
            //获取openid
            $res = curl($weixinurl);
            $token = json_decode($res, true);

            if (isset($token['openid'])) {
                $openid = $token['openid'];
                session("wxd_openid", $openid);
                $callback_url = session('callback_url');
                header("Location:$callback_url");
                exit(0);

            }
        } else {
            if (Env::get('app_debug')) {
                $this->redirect('http://sy.zoudingyong.com/codetoany/getcode.php?auk=demo1');
            } else {
                $this->redirect('http://sy.zoudingyong.com/codetoany/getcode.php?auk=mpquan');
            }

        }


    }

    public function index()
    {

        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }

        $config = RedisClass::get_vote_config_info();

        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }

        $source = input('source') ?: '';//来源
        //新增渠道来源
        if (!empty($source)) {
            if (!cookie('source')) {
                cookie('source', $source);
            }
        }

//        if (!Cache::get('member_' . $wxd_openid)) {
//            $member = Db::name('tp_member')->where('openid', $wxd_openid)->find();
//            if (empty($member)) {
//                Db::name('tp_member')->insert([
//                    'openid' => $wxd_openid,
//                    'source' => $source,
//                ]);
//            }
//            Cache::set('member_' . $wxd_openid, 1, 3600 * 24);
//        }
        //新增渠道来源
        $cate_id = input('cate_id') ?: 1;//赛区id
        $cate = RedisClass::get_cate_info($cate_id);
        $cate_list = RedisClass::get_cate_list();
        $page = input('page') ?: 1;//当前页数
        $pageSize = input('limit') ?: 10;//每页条数
        if (!is_numeric($page) || !is_numeric($pageSize)) return false;
        $limit_s = ($page - 1) * $pageSize;
        $limit_e = ($limit_s + $pageSize) - 1;

        if ($cate) {
            $HASH_PREFIX = 'vote:item_ids' . $cate_id;
        } else {
            $HASH_PREFIX = 'vote:item_ids';
        }

        $range = Cache::store('redis')->handler()->ZREVRANGE($HASH_PREFIX, $limit_s, $limit_e);
        $count = Cache::store('redis')->handler()->zCard($HASH_PREFIX);

        $pageCount = ceil($count / $pageSize); //总共多少页
        $list = array();
        if (!empty($range)) {
            foreach ($range as $qid) {
                $list[] = RedisClass::get_redis_item_info($qid);//获取hash表中所有的数据F
            }

//            $array_column = array_column($list, 'id');
//            $array_column_other = array_column($list, 'id');
//            array_multisort($array_column, SORT_DESC, $array_column_other, SORT_DESC, $list);
        }

        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];

            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };


        $config['item_num'] = $count;
        $banner = RedisClass::get_banner_info();
//        Cache::store('redis')->handler()->hset('vote:config', 'view_count', $config['view_count'] + 1); //总浏览数增加
        Cache::store('redis')->inc('view_count'); //总浏览数增加

        $view_count = Cache::store('redis')->get('view_count'); //实际总浏览数
        $total_vcount = Cache::store('redis')->get('total_vcount'); //实际总投票量

        $config['total_vcount'] = $total_vcount + $config['virtual_vcount'];
        $config['view_count'] = $view_count + $config['virtual_view_count'];

        return $this->fetch('activity', [
            'cate_id' => $cate_id,
            'cate' => $cate,
            'cate_list' => $cate_list,
            'list' => $list,
            'config' => $config,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);


    }



    /**
     * 报名
     * @return mixed
     */
    public function sign_area()
    {

        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }

        $cate_id = input('cate_id') ?: 1;//赛区id
        $cate = RedisClass::get_cate_info($cate_id);
        $count = Cache::store('redis')->handler()->zCard('vote:item_ids');
        $config = RedisClass::get_vote_config_info();
        $config['regdate'] = date('Y-m-d H:i:s', $config['regdate']);
        $config['regenddate'] = date('Y-m-d H:i:s', $config['regenddate']);
        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }
        $banner = RedisClass::get_banner_info();
        Cache::store('redis')->inc('view_count'); //总浏览数增加
        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };
        return $this->fetch('sign_area', [
            'config' => $config,
            'cate_id' => $cate_id,
            'cate' => $cate,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);


    }

    /**
     * 领取券列表
     * @return mixed
     */
    public function quan()
    {

        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }

        $cate_id = input('cate_id') ?: 1;//赛区id
        $cate = RedisClass::get_cate_info($cate_id);
        $count = Cache::store('redis')->handler()->zCard('vote:item_ids');
        $config = RedisClass::get_vote_config_info();
        $config['regdate'] = date('Y-m-d H:i:s', $config['regdate']);
        $config['regenddate'] = date('Y-m-d H:i:s', $config['regenddate']);
        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }
        $banner = RedisClass::get_banner_info();
        Cache::store('redis')->inc('view_count'); //总浏览数增加
        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
//                'appid' => 'wxa2affc91bc7c23e4',
                'appid' => sysconf('wechat_appid'),
//                'appsecret' => '7348ed2344f32939ab88872e151f94b3',
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };
        return $this->fetch('quan', [
            'config' => $config,
            'cate_id' => $cate_id,
            'cate' => $cate,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);


    }

    /**
     * 毛铺福利页
     * @return mixed
     */
    public function mpcoupon()
    {

        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }

        $cate_id = input('cate_id') ?: 1;//赛区id
        $cate = RedisClass::get_cate_info($cate_id);
        $count = Cache::store('redis')->handler()->zCard('vote:item_ids');
        $config = RedisClass::get_vote_config_info();
        $config['regdate'] = date('Y-m-d H:i:s', $config['regdate']);
        $config['regenddate'] = date('Y-m-d H:i:s', $config['regenddate']);
        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }
        $banner = RedisClass::get_banner_info();
        Cache::store('redis')->inc('view_count'); //总浏览数增加
        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };
        return $this->fetch('mpcoupon', [
            'config' => $config,
            'cate_id' => $cate_id,
            'cate' => $cate,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);


    }

    public function sign()
    {

        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }
        if (request()->isPost()) {
            $config = RedisClass::get_vote_config_info();
//            if ($config['regdate'] > time()) {
//                return json(['code' => 1, 'message' => '报名未开始']);
//            }
//            if ($config['regenddate'] < time()) {
//                return json(['code' => 1, 'message' => '报名已结束']);
//            }
            $cate_id = input('cate_id') ?: 0;

//            $reg = Db::name('tp_vote_item')->where('wechat_id', $wxd_openid)->where('cate_id', $cate_id)->find();
            $reg = Db::name('tp_vote_item')->where('phone', input('phone'))->where('cate_id', $cate_id)->find();
            if ($reg) {
                return json(['code' => 1, 'message' => '请勿重复报名!']);
            }

            $data['cate_id'] = input('cate_id') ?: 0;
            $data['item'] = input('item') ?: "";
            $data['sub_item'] = input('name');
            $data['phone'] = input('phone');
            $data['address'] = input('address');
            $data['addtime'] = time();
            $data['wechat_id'] = $wxd_openid;

            if (!empty(input('fileup'))) {
                foreach (input('fileup') as $key => $value) {
                    if ($key == 0) {
                        $data['startpicurl'] = base64_upload($value);
                    }
                    if ($key == 1) {
                        $data['startpicurl2'] = base64_upload($value);
                    }
                    if ($key == 2) {
                        $data['startpicurl3'] = base64_upload($value);
                    }
                    if ($key == 3) {
                        $data['startpicurl4'] = base64_upload($value);
                    }
                }
            }

            if ($result = Db::name('tp_vote_item')->insert($data)) {


                return json(['code' => 0, 'message' => '报名成功']);
            } else {
                return json(['code' => 1, 'message' => '报名失败,请重新报名!']);
            }

        } else {
            $cate_id = input('cate_id') ?: 1;//赛区id
            $cate = RedisClass::get_cate_info($cate_id);
            $config = RedisClass::get_vote_config_info();
            //开启流量限制
            if ($config['total_limit_status'] && !TokenBucket::get()) {
                $this->redirect($config['total_jump_url']);
            }
            $banner = RedisClass::get_banner_info();

            //分享朋友圈
            $current_url = $this->request->url(true);
            try {
                $wx_config = [
                    'appid' => sysconf('wechat_appid'),
                    'appsecret' => sysconf('wechat_appsecret'),
                ];
                $wechat = \WeChat\Script::instance($wx_config);
                $signPackage = $wechat->getJsSign($current_url);
            } catch (\Exception $e) {
                $signPackage = [
                    'appId' => sysconf('wechat_appid'),
                    'timestamp' => '',
                    'nonceStr' => '',
                    'signature' => '',
                ];
            };
//            $item = Db::name('tp_vote_item')->where('wechat_id', $wxd_openid)->find();
            $item = [];

            return $this->fetch('sign', [
                'item' => $item,
                'cate_id' => $cate_id,
                'cate' => $cate,
                'config' => $config,
                'banner' => $banner,
                'current_url' => $current_url,
                'signPackage' => $signPackage,
            ]);
        }

    }

    public function sign2()
    {

        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }
        if (request()->isPost()) {
            $config = RedisClass::get_vote_config_info();

            $cate_id = input('cate_id') ?: 0;

//            $reg = Db::name('tp_vote_item')->where('wechat_id', $wxd_openid)->where('cate_id', $cate_id)->find();
            $reg = Db::name('tp_vote_item')->where('phone', input('phone'))->where('cate_id', $cate_id)->find();
            if ($reg) {
                return json(['code' => 1, 'message' => '请勿重复报名!']);
            }

            $data['cate_id'] = input('cate_id') ?: 0;
            $data['item'] = input('item') ?: "";
            $data['sub_item'] = input('name');
            $data['phone'] = input('phone');
            $data['address'] = input('address');
            $data['addtime'] = time();
            $data['wechat_id'] = $wxd_openid;

            if (!empty(input('fileup'))) {
                foreach (input('fileup') as $key => $value) {
                    if ($key == 0) {
                        $data['startpicurl'] = base64_upload($value);
                    }
                    if ($key == 1) {
                        $data['startpicurl2'] = base64_upload($value);
                    }
                    if ($key == 2) {
                        $data['startpicurl3'] = base64_upload($value);
                    }
                    if ($key == 3) {
                        $data['startpicurl4'] = base64_upload($value);
                    }
                }
            }

            if ($result = Db::name('tp_vote_item')->insert($data)) {


                return json(['code' => 0, 'message' => '报名成功']);
            } else {
                return json(['code' => 1, 'message' => '报名失败,请重新报名!']);
            }

        } else {
            $cate_id = input('cate_id') ?: 1;//赛区id
            $cate = RedisClass::get_cate_info($cate_id);
            $config = RedisClass::get_vote_config_info();
            //开启流量限制
            if ($config['total_limit_status'] && !TokenBucket::get()) {
                $this->redirect($config['total_jump_url']);
            }
            $banner = RedisClass::get_banner_info();

            //分享朋友圈
            $current_url = $this->request->url(true);
            try {
                $wx_config = [
                    'appid' => sysconf('wechat_appid'),
                    'appsecret' => sysconf('wechat_appsecret'),
                ];
                $wechat = \WeChat\Script::instance($wx_config);
                $signPackage = $wechat->getJsSign($current_url);
            } catch (\Exception $e) {
                $signPackage = [
                    'appId' => sysconf('wechat_appid'),
                    'timestamp' => '',
                    'nonceStr' => '',
                    'signature' => '',
                ];
            };
//            $item = Db::name('tp_vote_item')->where('wechat_id', $wxd_openid)->find();
            $item = [];

            return $this->fetch('sign2', [
                'item' => $item,
                'cate_id' => $cate_id,
                'cate' => $cate,
                'config' => $config,
                'banner' => $banner,
                'current_url' => $current_url,
                'signPackage' => $signPackage,
            ]);
        }

    }


    public function activity()
    {
        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }
        $cate_id = input('cate_id') ?: 1;//赛区id
        $config = RedisClass::get_vote_config_info();
        Cache::store('redis')->inc('view_count'); //总浏览数增加
        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }
        $banner = RedisClass::get_banner_info();

        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };

        return $this->fetch('activity', [
            'cate_id' => $cate_id,
            'config' => $config,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);
    }

    public function chengrenli()
    {
        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }
        $cate_id = input('cate_id') ?: 1;//赛区id
        $config = RedisClass::get_vote_config_info();
        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }
        $banner = RedisClass::get_banner_info();

        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };
        Cache::store('redis')->inc('view_count'); //总浏览数增加

        return $this->fetch('chengrenli', [
            'cate_id' => $cate_id,
            'config' => $config,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);
    }

    public function gaokaozhiyuan()
    {
        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }
        $cate_id = input('cate_id') ?: 1;//赛区id
        $config = RedisClass::get_vote_config_info();
        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }
        $banner = RedisClass::get_banner_info();

        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };
        Cache::store('redis')->inc('view_count'); //总浏览数增加

        return $this->fetch('gaokaozhiyuan', [
            'cate_id' => $cate_id,
            'config' => $config,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);
    }

    public function shujiashijian()
    {
        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }
        $cate_id = input('cate_id') ?: 1;//赛区id
        $config = RedisClass::get_vote_config_info();
        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }
        $banner = RedisClass::get_banner_info();

        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };
        Cache::store('redis')->inc('view_count'); //总浏览数增加

        return $this->fetch('shujiashijian', [
            'cate_id' => $cate_id,
            'config' => $config,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);
    }

    public function huigu()
    {
        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }
        $cate_id = input('cate_id') ?: 1;//赛区id
        $config = RedisClass::get_vote_config_info();
        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }
        $banner = RedisClass::get_banner_info();

        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };
        Cache::store('redis')->inc('view_count'); //总浏览数增加

        $list = Db::name('tp_article')->order('sort desc,article_id desc')->select();

        return $this->fetch('huigu', [
            'list' => $list,
            'cate_id' => $cate_id,
            'config' => $config,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);
    }

    public function about()
    {
        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }
        $cate_id = input('cate_id') ?: 1;//赛区id
        $config = RedisClass::get_vote_config_info();
        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }
        $banner = RedisClass::get_banner_info();

        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };
        Cache::store('redis')->inc('view_count'); //总浏览数增加

        return $this->fetch('about', [
            'cate_id' => $cate_id,
            'config' => $config,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);
    }

    public function busy()
    {
        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }
        $cate_id = input('cate_id') ?: 1;//赛区id
        $config = RedisClass::get_vote_config_info();
        $banner = RedisClass::get_banner_info();

        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };

        return $this->fetch('busy', [
            'cate_id' => $cate_id,
            'config' => $config,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);
    }

    public function area()
    {
        $wxd_openid = session("wxd_openid");
        if (!$wxd_openid) {
            $this->auth($this->request->url(true));
        }
        $config = RedisClass::get_vote_config_info();
        //开启流量限制
        if ($config['total_limit_status'] && !TokenBucket::get()) {
            $this->redirect($config['total_jump_url']);
        }
        $banner = RedisClass::get_banner_info();

        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };

        return $this->fetch('area', [
            'config' => $config,
            'banner' => $banner,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);
    }



    /**
     * 上传图片
     * @param $width
     * @return \think\response\Json
     */
    public function upload($width = 600)
    {
        $file = $this->request->file('file');
        if (empty($file)) {
            return json(['code' => 0, 'msg' => '请上传文件']);
        }
        $result = upload($file, $width);

        if ($result) {
            return json(['code' => 1, 'msg' => $result['url']]);
        } else {
            return json(['code' => 0, 'msg' => '']);
        }
    }

    public function cos_sts()
    {

        // 获取临时密钥，计算签名
        $tempKeys = cossts();
        return json(['code' => 0, 'message' => 'cos鉴权', 'data' => $tempKeys]);

    }
}
